package cn.ricecode.mybatis.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;
import com.github.pagehelper.PageInfo;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 场景，当需要拷贝PageInfo里面的List的时候，但是又要保留PageInfo
 * @author LiuZhulan
 */
public class PageInfoExecutionUtils {

    public static <U, T> PageInfo<U> map(PageInfo<T> pageInfo, Function<? super T, ? extends U> converter) {
        PageInfo<U> info = (PageInfo) JSON.parseObject(JSON.toJSONString(pageInfo), new TypeReference<PageInfo<U>>() {
        }, new Feature[0]);
        info.setList((List)pageInfo.getList().stream().map(converter).collect(Collectors.toList()));
        return info;
    }

}
